using System;
using System.Diagnostics;
using System.Windows.Forms;

using FreeFlow.Administration;

namespace FreeFlowAdministrator
{
	/// <summary>
	/// Summary description for ProcedureAdaptor.
	/// </summary>
	public class ProcedureAdaptor : Adaptor, IRefreshable, IDeletable
	{
    private Procedure procedure;
		public ProcedureAdaptor(Procedure procedure)
		{
      this.procedure = procedure;
		}

    public override void AddChildren(TreeNode node)
    {
      // maps
      TreeNode mapsNode = TreeNodeFactory.CreateTreeNode(new MapsAdaptor(procedure.Maps));
      node.Nodes.Add(mapsNode);
      
      // forms
      if (procedure.Forms.Count > 0)
      {
        TreeNode formsNode = TreeNodeFactory.CreateTreeNode(new FormsAdaptor(procedure.Forms));
        node.Nodes.Add(formsNode);
      }
      
      // roles
      TreeNode rolesNode = TreeNodeFactory.CreateTreeNode(new RolesAdaptor(procedure.Roles));
      node.Nodes.Add(rolesNode);
      
      // versions
      TreeNode versionsNode = TreeNodeFactory.CreateTreeNode(new ProcedureVersionsAdaptor(procedure.Versions));
      node.Nodes.Add(versionsNode);
      
      // scripts
      if (procedure.Scripts.Count > 0)
      {
        TreeNode scriptsNode = TreeNodeFactory.CreateTreeNode(new ScriptsAdaptor(procedure.Scripts));
        node.Nodes.Add(scriptsNode);
      }

      // designer log
      if (procedure.DesignerLog.Count > 0)
      {
        TreeNode designerLogNode = TreeNodeFactory.CreateTreeNode(new DesignerLogAdaptor(procedure.DesignerLog));
        node.Nodes.Add(designerLogNode);
      }
    }

    public override int ImageIndex
    {
      get
      {
        return 8;
      }
    }

    public override string Text
    {
      get
      {
        return procedure.Name;
      }
    }

    public override Control GetRenderer()
    {
      ProcedureControl control = new ProcedureControl();
      control.LoadProcedure(procedure);
      control.ItemDeleted += new EventHandler(TriggerItemDeleted);
      control.RefreshRequired += new EventHandler(TriggerRefreshRequired);
      return control;
    }

    public override object Object
    {
      get
      {
        return procedure;
      }
    }

    #region IRefreshable Members

    public void Refresh()
    {
      procedure.Refresh();
    }

    #endregion
    #region IDeletable Members

    public void Delete()
    {
      procedure.Delete();
    }

    #endregion
  }
}
